var searchPrompt = "Search by Keywords...";

/*
 * Decide the redirect action after submit a form.
 * @param formId
 * @param action
 * @param hiddenInputName used to judge which input element used to transmit the
 *       action after submit the form.
 */
function afterSubmitRedirect(formId, formAction, submitAction, submitActionFlagId) {
    $("#" + submitActionFlagId).attr("value", submitAction);
    $("#" + formId).attr("action", formAction);
    $("#" + formId).submit();
}

// ********** For List Function Begin ********** //
var itemParams = "";

function itemHover(item) {
    $("div.data_tbody div").removeClass("hover");
    $(item).addClass("hover");
}

function itemClick(item, params) {
    $("div.data_tbody div").removeClass("active");
    $(item).addClass("active");
    itemParams = params;
}

function itemMouseOut(item) {
    $(item).removeClass("hover");
}

function clickAction(action, isBlank) {
    if (itemParams == "") {
        alert("Please select an item.");
    } else if (!isBlank) {
        var currentUrl = "&from=" + escape(window.location.href);
        itemParams += currentUrl;
        window.location.href = action + ".html?" + itemParams;
    } else {
        window.open(action + ".html?" + itemParams);
    }
}


function briefMode() {
    $("#dataList").removeClass("data_tbody");
    $("#dataList").addClass("data_tbody brief");
    $("#full").removeClass("active");
    $("#brief").addClass("active");
    document.cookie = "displayMode=brief";
}

function fullMode() {
    $("#dataList").removeClass("data_tbody brief");
    $("#dataList").addClass("data_tbody");
    $("#brief").removeClass("active");
    $("#full").addClass("active");
    document.cookie = "displayMode=full";
}

function initDisplayMode() {
    var modeKey = "displayMode=";
    var allCookies = document.cookie;
    var position = allCookies.indexOf(modeKey);
    if (position != -1) {
        var start = position + modeKey.length;
        var end = allCookies.indexOf(";", start);
        modeValue = allCookies.substring(start, end);
        if (modeValue == "brief") {
            briefMode();
        }
        if (modeValue == "full") {
            fullMode();
        }
    }
}
$(document).ready(function() {
    initDisplayMode();

    var searchPrompt = "Search by Keywords...";

    $("#keywords").blur(function () {
        if ($("#keywords").val() == "") {
            $("#keywords").val(searchPrompt);
        }
    });

});
// ********** For List Function End ********** //
function searchClean(search) {
    if ($(search).val() == "Search by Keywords...") {
        $(search).val("");
    }
}
function sortByColumn() {
    var thisURL = document.URL;
    tmpUPage = thisURL.split("/");
    thisUPage = tmpUPage[tmpUPage.length - 1];
    thisUPage = thisUPage.substr(0, thisUPage.indexOf("?"))
    var sortColumn = event.srcElement.id;
    var column = sortColumn.replace("sort", "");
    var firstSort = $("#" + sortColumn).attr("class");
    var sortBy = false;
    if (firstSort == "id") {
        sortBy = true;
    }
    if (firstSort == "") {
        sortBy = true;
    }
    if (firstSort == "id_move_up") {
        afterSort = "id";
        sortBy = false;
    }
    window.location.href = thisUPage + "?sortColumn=" + column + "&sortBy=" + sortBy;

}

function sort() {
    var thisURL = document.URL;
    var tmpUPage = thisURL.split("/");
    var thisUPage = tmpUPage[tmpUPage.length - 1];
    var thisUPage = thisUPage.substr(0, thisUPage.indexOf("?"))
    var isSplit = false;
    var queryStr = thisURL.split("?")[1] ? thisURL.split("?")[1] : "";
    var items = queryStr == "" ? [] : queryStr.split("&");
    var newItems = [];
    for (var i = 0; i < items.length; i++) {
        if (items[i].indexOf("sortColumn=") == -1 && items[i].indexOf("sortBy=") == -1) {
            newItems.push(items[i]);
        }
    }
    $(".sort").hover(
            function() {
                $(this).addClass("hover_th")
            },
            function() {
                if ($(this).hasClass("hover_th")) $(this).removeClass("hover_th");
            })
            .click(function() {
        var col = $(this).attr("column");
        if ($(this).hasClass("id_move_up")) {
            sortQueryStr = "sortColumn=" + col + "&sortBy=" + "desc";
        } else {
            sortQueryStr = "sortColumn=" + col + "&sortBy=" + "asc";
        }

        window.location.href = thisUPage + "?" + newItems.join("&") + "&" + sortQueryStr;
    });
}

/**
 * This method can be used to get the param value from the url by the param name.
 * NOTE: Currently, the param need to put on the last position.
 */
function getParamFromUrl(paramName) {
    var url = document.location.href;

    if (url.indexOf(paramName) == -1) {
        return "";
    }
    var begin = url.indexOf(paramName) + paramName.length + 1;
    var end = url.length;
    return url.substring(begin, end);
}

function readMessageFromUrl() {
    var message = getParamFromUrl("message");
    if (message != "") {
        // Convert blank.
        message = message.split("%20").join(" ");
        $("div.error h3").html(message);
    }
}

// ********** Change the tabs on Edit or View Pages: Begin ********** //
function navToQs() {
    $("#general").hide();
    $("#whereUsed").hide();
    $("#questions").show();
    $("div.sub_nav img").each(function() {
        var imgSrc = $(this).attr("src");
        if ($(this).attr("alt") == "questions" && imgSrc.indexOf("_off") != -1) {
            $(this).attr("src", imgSrc.replace("_off", "_on"));
        } else if ($(this).attr("alt") != "questions" && imgSrc.indexOf("_on") != -1) {
            $(this).attr("src", imgSrc.replace("_on", "_off"));
        }
    });
}

function navToGeneral() {
    $("#questions").hide();
    $("#whereUsed").hide();
    $("#general").show();
    $("div.sub_nav img").each(function() {
        var imgSrc = $(this).attr("src");
        if ($(this).attr("alt") == "general" && imgSrc.indexOf("_off") != -1) {
            $(this).attr("src", imgSrc.replace("_off", "_on"));
        } else if ($(this).attr("alt") != "general" && imgSrc.indexOf("_on") != -1) {
            $(this).attr("src", imgSrc.replace("_on", "_off"));
        }
    });
}

function navToWhereUsed() {
    $("#questions").hide();
    $("#general").hide();
    $("#whereUsed").show();
    $("div.sub_nav img").each(function() {
        var imgSrc = $(this).attr("src");
        if ($(this).attr("alt") == "whereUsed" && imgSrc.indexOf("_off") != -1) {
            $(this).attr("src", imgSrc.replace("_off", "_on"));
        } else if ($(this).attr("alt") != "whereUsed" && imgSrc.indexOf("_on") != -1) {
            $(this).attr("src", imgSrc.replace("_on", "_off"));
        }
    });
}
// ********** Change the tabs on Edit or View Pages: End ********** //
var greyButton = function(obj) {
    if ($(obj).html() == null) {
        return false;
    }
    var imgSrc = $(obj).children().attr("src");

    if (imgSrc.indexOf("_light") == -1) {
        $(obj).children().attr("src", imgSrc.replace(".", '_light.'));
        $(obj).children().removeClass("btn");
    }
}
var displayButton = function(obj) {
    if ($(obj).html() == null) {
        return false;
    }
    var imgSrc = $(obj).children().attr("src");
    if (imgSrc.indexOf("_light") != -1) {
        $(obj).children().attr("src", imgSrc.replace("_light.", '.'));
        $(obj).children().addClass("btn");
    }
}
var IsURL = function(str_url){
          str_url=str_url.toLowerCase();          
		  var strRegex = "^((https|http|ftp|rtsp|mms)://)"
		  		+ "(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
		        + "(([0-9]{1,3}\.){3}[0-9]{1,3}" 
		        + "|" 
		        + "([0-9a-z_!~*'()-]+\.)*" 
		        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." 
		        + "[a-z]{2,6})" 
		        + "(:[0-9]{1,4})?";
		  var re=new RegExp(strRegex);		  	 
		  if (re.test(str_url)){
		            return (true);
		   }else{
		            return (false);
		   }
		}